package com.satan.fmmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.satan.fmmall.entity.Orders;
import com.satan.fmmall.entity.ProductComments;
import com.satan.fmmall.mapper.OrderItemMapper;
import com.satan.fmmall.mapper.OrdersMapper;
import com.satan.fmmall.mapper.ProductCommentsMapper;
import com.satan.fmmall.service.ProductCommentsService;
import com.satan.fmmall.vo.PageHelperVo;
import com.satan.fmmall.vo.ProductCommentsVo;
import com.satan.fmmall.vo.ResultVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ProductCommentsServiceImpl
 * @Description TODO
 * @date 2022/4/10 23:16
 * @Version 1.0
 */
@Service
public class ProductCommentsServiceImpl extends ServiceImpl<ProductCommentsMapper, ProductComments> implements ProductCommentsService {
    @Resource
    private ProductCommentsMapper productCommentsMapper;
    @Resource
    private OrdersMapper ordersMapper;
    @Resource
    private OrderItemMapper orderItemMapper;
    @Override
    public ResultVo listCommentsByProductId(String productId,int pageNum,int limit) {
        QueryWrapper<ProductComments> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("product_id",productId);
        Long count = productCommentsMapper.selectCount(queryWrapper);
        Long pageCount= count%limit==0?count/limit:count/limit+1;
        int start=(pageNum-1)*limit;
        List<ProductCommentsVo> productCommentsVos = productCommentsMapper.selectCommentsByProductId(productId, start, limit);
        PageHelperVo<ProductCommentsVo> pageHelperVo=new PageHelperVo<ProductCommentsVo>(count,pageCount,productCommentsVos);
        return ResultVo.success(pageHelperVo);
    }

    @Override
    public ResultVo getCommentsCountByProductId(String productId) {
        //查询评论数量
        LambdaQueryWrapper<ProductComments> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductComments::getProductId,productId);
        Long total = productCommentsMapper.selectCount(queryWrapper);
        //查询好评评价数
        queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductComments::getCommType ,1);
        Long goodTotal = productCommentsMapper.selectCount(queryWrapper);
        //查询中评评价数
        queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductComments::getCommType ,0);
        Long midTotal = productCommentsMapper.selectCount(queryWrapper);
        //查询差评评价数
        queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductComments::getCommType ,-1);
        Long badTotal = productCommentsMapper.selectCount(queryWrapper);
        //计算好评率
        double percent=0;
        if (total>0){
            DecimalFormat df   = new DecimalFormat("0.00");
            percent=(double)goodTotal/(double)total*100;
            percent= Double.parseDouble(df.format(percent));
        }
        Map<String, Object> map=new HashMap<>();
        map.put("total",total);
        map.put("goodTotal",goodTotal);
        map.put("midTotal",midTotal);
        map.put("badTotal",badTotal);
        map.put("percent",percent);
        return ResultVo.success(map);
    }

    @Override
    @Transactional(isolation = Isolation.SERIALIZABLE)
    public void insertProductComments(List<ProductComments> productCommentsList,String orderId) throws SQLException {
        productCommentsList.forEach(item->{
            item.setIsShow(1);
            Integer commLevel=item.getCommLevel();
            if (commLevel==1){
                item.setCommType(1);
            }else if (commLevel==2){
                item.setCommType(0);
            }else {
                item.setCommType(-1);
            }
        });
        System.out.println("productCommentsList = " + productCommentsList);
        boolean batch = saveBatch(productCommentsList);
        Orders orders=new Orders();
            orders.setOrderId(orderId);
        ordersMapper.updateById(orders);
//        List<String> orderItemIdList = productCommentsList.stream().map(ProductComments::getOrderItemId).collect(Collectors.toList());
//        List<String> orderIdList = orderItemMapper.selectBatchIds(orderItemIdList).stream().map(OrderItem::getOrderId).collect(Collectors.toList());
//        orderIdList.forEach(item->{
//            Orders orders=new Orders();
//            orders.setOrderId(item);
//            orders.setStatus("5");
//            ordersMapper.updateById(orders);
//        });



    }

}
